﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using System.Threading;


//闹钟类
public class CLOCK
{
    //闹钟滴答事件
    public event EventHandler Tick;

    //闹钟响铃事件
    public event EventHandler Alarm;

    //播放的音频：
    private SoundPlayer musicOfTick;
    private SoundPlayer musicOfAlarm;

    // 运行的时间（秒）
    private int time;

    //闹钟构造函数：
    public CLOCK(int Time,string pathOfTick,string pathOfAlarm)
    {
        time = Time;
        //初始化音频播放器：
        musicOfTick = new SoundPlayer(pathOfTick);
        musicOfAlarm = new SoundPlayer(pathOfAlarm);
    }

  //闹钟开始：
    public void Start()
    {
        for (int i = 1; i <= time; i++)
        {
            Thread.Sleep(1000); 
            GoTick(); // 闹钟滴答响，每隔一秒响一次

            if (i == time)
            {
                GoAlarm(); //闹钟响铃
            }
        }
    }

    //  闹钟滴答响：
    protected void GoTick()
    {
        musicOfTick.Play();
        Tick?.Invoke(this, EventArgs.Empty);
    }

    // 闹钟响铃：
    protected void GoAlarm()
    {
        musicOfAlarm.Play();
        Alarm?.Invoke(this, EventArgs.Empty);
    }
}

namespace Clock
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("我将设置一个___秒的闹钟");
            int alarmTime = int.Parse(Console.ReadLine());
            
            //音频文件路径：
            string tickPath = "music\\tic.wav.wav";
            string alarmPath = "music\\didi.wav.wav";

            // 创建闹钟实例
            CLOCK clock = new CLOCK(alarmTime, tickPath, alarmPath);

            // 订阅 Tick 事件
            clock.Tick += (sender, e) =>
            {
                Console.WriteLine($"嘀嗒滴答----当前的时间是：{DateTime.Now.ToString("HH:mm:ss")}");
            };

            // 订阅 Alarm 事件
            clock.Alarm += (sender, e) =>
            {
                Console.WriteLine($"闹钟响了！当前的时间是：{DateTime.Now.ToString("HH:mm:ss")}");
            };

            Console.WriteLine("闹钟开始启动！");
            clock.Start(); // 启动闹钟
           
            //方便观察输出面板
            Console.ReadLine();
        }
    }
}
